<script lang="ts" setup>
import { Model } from "./model";

Model.avatar.type = "avatar";
const PageConfig = {
  //被操作实体的名称
  entity: "user",
  entityName: "用户",
  // 表单是否分组
  formGrouped: true,
  // 未命名分组的默认名称
  groupName_default: "其他信息",
  api: {
    //获取详情
    detail: "/user/detail",
    //新增
    add: "/user/add",
    //修改
    edit: "/user/edit",
  },
  // 展示重置密码按钮
  showResetPasswordBtn: true,
};
const reactive_Model = reactive(Model);
const initOK = ref(false);

const getData = async () => {
  try {
    const res = await $fetch(`/api/role/all`);
    reactive_Model.roles.options = res.data.map((item) => ({
      label: item.name,
      value: item._id,
    }));

    const res_position = await $fetch(`/api/position/all`);

    reactive_Model.positions.options = res_position.data.map((item) => ({
      label: item.name,
      value: item._id,
    }));

    reactive_Model.gender.options = await getDicList("gender");
  } catch (error) {}
};

const tableDataFormate = (row: any) => {
  if (row.department !== undefined) {
    row.department_desc = getTargetFromTree(
      reactive_Model.department.treeData,
      "code",
      row.department,
      "label"
    );
  }
  return row;
};

onMounted(async () => {
  const res_department: {
    [key: string]: any;
  }[] = await $fetch(`/api/system/list`, {
    query: {
      name: "department",
    },
  });
  if (Array.isArray(res_department) && res_department.length > 0) {
    reactive_Model.department.treeData = JSON.parse(res_department[0].content);
  }

  getData();

  initOK.value = true;
});
</script>
<template>
  <S-comMangeInfo
    v-if="initOK"
    :Model="reactive_Model"
    :PageConfig="PageConfig"
    :tableDataFormate="tableDataFormate"
  >
  </S-comMangeInfo>
</template>
